<?php
/**
 * Created by PhpStorm.
 * User: stupboy
 * Date: 2018/4/10
 * Time: 15:13
 * Mail:87658989@qq.com
 */

namespace app\base\service;

use think\Db;

class Dayu
{
    /**
     * 短信加入队列
     * @param string $mobile
     * @param array $data
     * @param string $templateCode
     */
    public function addToList($mobile='',$data = [],$templateCode = 'SMS_130911261'){
        $arr = [
            'mobile'=>$mobile,
            'data'  =>$data,
            'code'  =>$templateCode
        ];
        Redis::getInstance()->push('send_sms_list',serialize($arr));
    }

    /**
     * 发送短信
     * @param string $mobile
     * @param array $data
     * @param string $templateCode
     */
    public function sendMsg($mobile='',$data = [],$templateCode = 'SMS_130911261'){
        $params          = [];
        // *** 需用户填写部分 ***
        // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
        $accessKeyId     = "LTAIBVa52ezFVxQQ";
        $accessKeySecret = "yH32TJNVAverIZdErQk3nAtwIVH5v6";
        // fixme 必填: 短信接收号码
        $params["PhoneNumbers"] = $mobile;
        // fixme 必填: 短信签名，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
        $params["SignName"]     = "晋江出行";
        // fixme 必填: 短信模板Code，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
        $params["TemplateCode"] = $templateCode;
        // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
        $params['TemplateParam'] = $data;
        // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
        if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
        }
        // 初始化SignatureHelper实例用于设置参数，签名以及发送请求
        $helper = new \Tool\signaturehelper();
        // 此处可能会抛出异常，注意catch
        $content = $helper->request(
            $accessKeyId,
            $accessKeySecret,
            "dysmsapi.aliyuncs.com",
            array_merge($params, array(
                "RegionId" => "cn-hangzhou",
                "Action" => "SendSms",
                "Version" => "2017-05-25",
            ))
        );
        return $content;
    }
}